<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>帮助教程管理</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
    <style>
        .technical{
            display: none;
        }
    </style>
</head><body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
            <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/help/help/save.do">
                <input type="hidden" name="id" value="${help.id}"/>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">类型<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <select id="select_type" name="type">
                                <option value="0" <c:if test="${help.type == 0}">selected</c:if> >教程</option>
                                <option value="1" <c:if test="${help.type == 1}">selected</c:if> >技术支持</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item non-technical">
                    <label class="layui-form-label">上级教程<span class="f-verify-red">*</span></label>
                    <div class="layui-input-block">
                        <input id="parentId" name="parentId" value="${parentHelp.id}" hidden>
                        <c:if test="${empty help.id}">
                            <input type="text" class="layui-input" value="${parentHelp.title}" id="selectParent" readonly="readonly" onclick="clickParent()" />
                        </c:if>
                        <c:if test="${not empty help.id}">
                            <input type="text" class="layui-input" value="${parentHelp.title}" id="selectParent" readonly="readonly" />
                        </c:if>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label technical">排序<span class="f-verify-red">*</span></label>
                        <label class="layui-form-label non-technical">教程序号<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <c:if test="${empty help.id}">
                                <input type="text" name="serialNumber" lay-verify="serialNumber" autocomplete="off" class="layui-input" value="${sort}" id="serialNumber">
                            </c:if>
                            <c:if test="${not empty help.id}">
                                <input type="text" name="serialNumber" lay-verify="serialNumber" autocomplete="off" class="layui-input" value="${help.serialNumber}" id="serialNumber">
                            </c:if>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label technical">标题<span class="f-verify-red">*</span></label>
                    <label class="layui-form-label non-technical">教程标题<span class="f-verify-red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="title" autocomplete="off" class="layui-input" value="${help.title}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label technical">功能详情<span class="f-verify-red">*</span></label>
                    <label class="layui-form-label non-technical">后台内容<span class="f-verify-red">*</span></label>
                    <div class="layui-input-block">
                        <script id="ueditor1" type="text/plain"></script>
                        <div style="display: none" id="contentView">${help.content}</div>
                        <input type="hidden" name="content" id="content" lay-verify="content" />
                    </div>
                </div>
                <div class="layui-form-item non-technical">
                    <label class="layui-form-label">手机端内容<span class="f-verify-red">*</span></label>
                    <div class="layui-input-block">
                        <script id="ueditor2" type="text/plain"></script>
                        <div style="display: none" id="contentView2">${help.frontContent}</div>
                        <input type="hidden" name="frontContent" id="content2" lay-verify="frontContent" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                        <a href="javascript:history.back();" class="layui-btn layui-btn-primary">取消</a>
                    </div>
                </div>
            </form>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
</div>

<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.config.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/ueditor.all.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadCI.js"></script>
<script type="text/javascript" src="${ctxStatic}/UEditor/myplugin/uploadVideo.js"></script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" src="${ctxStatic}/UEditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
    var ue = UE.getEditor('ueditor1');
    var ue2 = UE.getEditor('ueditor2');
    $(function() {
        chmod(${help.type});

        ue.addListener('ready', function() {
            this.setHeight(500);
            this.setContent($("#contentView").html());
        });
        ue.addListener('blur', function() {
            $("#contentView").html(ue.getContent());
        });

        ue2.addListener('ready', function() {
            this.setHeight(500);
            this.setContent($("#contentView2").html());
        });
        ue2.addListener('blur', function() {
            $("#contentView2").html(ue2.getContent());
        });

        layui.use(['form'], function () {
            var form = layui.form;

            //自定义验证规则
            form.verify({
                serialNumber: function (value) {
                    var reg = /^([1-9]+)$|([1-9]+)((\.)?)([0-9]+)$|([1-9]+)((\.)?)([0-9]+)((\.)?)([0-9]+)$/;
                    if (value == "") {
                        return "请输入序号";
                    }
                    /*else if (!reg.test(value)) {
                        return "请输入正确格式的序号";
                    }*/
                },
                title: function (value) {
                    if (value == "") {
                        return '请输入标题';
                    }
                },
                content: function (value) {
                    var content = $("#contentView").html();
                    if (content == "") {
                        return "请输入后端内容";
                    }
                },
                frontContent: function (value) {
                    var st = $("#select_type").val();
                    if(st == 1){
                        return;
                    }
                    var content = $("#contentView2").html();
                    if (content == "") {
                        return "请输入前端内容";
                    }
                }
            });

            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    $("#content").val(ue.getContent().replace(/&quot;/gi, ""));
                    $("#content2").val(ue2.getContent().replace(/&quot;/gi, ""));
                    $.post('${ctx}/help/help/save.do', $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/help/help/list.do";
                            });
                        } else {
                            util.layerMsgError(res.description)
                        }
                    });
                });
                return false;
            });

            form.on('select',function (data) {
                chmod(data.value);
            });
        });
    });

    function clickParent() {
        var id = $("#parentId").val() || '0';
        layer.open({
            type: 2,
            area: ["400px", "300px"],
            title: "选择父级",
            maxmin: true,
            content: "${ctx}/help/help/" + id + "/selectParent.do",
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                top_iframe = '_parent';
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                var parent = iframeWin.contentWindow.doSubmit();
                if (parent != false){
                    $('#parentId').val(parent.id);
                    $('#selectParent').val(parent.name);
                    callBack(parent.id, '#serialNumber');
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);

                }
            },
            cancel: function (index) {
            }
        });
    }

    function callBack(parentId, container) {
        $.post("${ctx}/help/help/getMaxSort.do", {"parentId" : parentId || '0'}, function (res) {
            $(container).val(res.data);
        })
    }

    // 文本编辑器图片上传
    function uEditorUploadCI(editor) {
        txz.openSelImg({
            min: 1,
            type: '<%=TargetType.HELP.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }

    function uEditorUploadCIVideo(editor) {
        txz.openSelVideo({
            min: 1,
            type: '<%=TargetType.HELP.getCode()%>',
            cb: function (imgs) {
                if (imgs.length) {
                    for (var i = 0, item; i < imgs.length; i++) {
                        item = imgs[i];
                        editor.focus();
                        editor.execCommand('inserthtml', item.htmlStr);
                    }
                }
            }
        })
    }

    function chmod(value){
        if(value == 0){
            //显示技术支持
            $(".technical").hide();
            $(".non-technical").show();
        }else if(value == 1) {
            //隐藏部分内容
            $(".technical").show();
            $(".non-technical").hide();
        }
    }
</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '帮助教程管理',
            href:'javascript:history.back();',
        },{
            name: '${help.id == null ? '新增' : '编辑'}教程',
            curr: true
        }],
        btns:[{
            type:'back'
        }]
    });
</script>
</body>
</html>